package cn.kz.hrm.web.controller;

import cn.kz.hrm.domain.Pay;
import cn.kz.hrm.domain.Shopcar;
import cn.kz.hrm.query.PayQuery;
import cn.kz.hrm.service.IPayService;
import cn.kz.hrm.util.AjaxResult;
import cn.kz.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    public IPayService payService;


    /**
     * 生成订单，订单的默认的状态为0.
     * @param shopcar
     * @return
     */
    @RequestMapping(value="/payOrder",method=RequestMethod.POST)
    public AjaxResult delete(@RequestBody Shopcar shopcar){
        try {
            //没有前台准备的假数据。
            Shopcar shopcar1 = new Shopcar();
            shopcar1.setEmployeeId(1L);
            shopcar1.setPrice((float) 1000.0);
            shopcar1.setCourseId(1L);
            shopcar1.setName("JAVA从入门到放弃！");
            String payData = payService.payOrderAndPay(shopcar1);
            return AjaxResult.me().setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("支付失败！！！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param pay  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Pay pay){
        try {
            if(pay.getId()!=null){
                payService.updateById(pay);
            }else{
                payService.insert(pay);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            payService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Pay get(@PathVariable("id")Long id)
    {
        return payService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Pay> list(){

        return payService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Pay> json(@RequestBody PayQuery query)
    {
        Page<Pay> page = new Page<Pay>(query.getPage(),query.getRows());
        page = payService.selectPage(page);
        return new PageList<Pay>(page.getTotal(),page.getRecords());
    }
}
